added Request Animation Frame
refactoring code