Code refactored. Requestanimframe added.