organized code and minor bug fix