Removed unneccessary code and global variables. fixed flickering.