massive refactor, improved functionality