cleanup the code, remove duplication, etc.