Total refactor, added documentation