Organized code, added more logging