Refactured code & added AlertDialog