refactor out some code and fix some alignement issues