Added viewAppear, refactored code