removed dead code
refactored to clean up duplicated and messy code