fixed minor bug and organized code