Added code to handle lifecycle events