code optimization and vertical rebound fix