In addition to what the other two have suggested, one specific thing you can do is try removing each database query to its own subroutine. Since each statement handle is its own logical unit, and returns a different thing, it would be easy to write them as individual subroutines that accept parameters to look for (like $MornIn and $MornOut), and return either success for an UPDATE or specific values for a SELECT.