more refactoring for both speed and type safety :D