clean up code and eliminate duplicate code in version 0.3