Added orientation change handling. Other minor bug fixed and cleaned up code a bit