refactored and some other clean up