removed some code duplication and such