major code refactoring - haven't tested