Cleanup code and move timer functions to named functions