Made minor modification and code restructuring