cleaned up code, removed duplication