Code cleanup

Improved delay calculating