Rewriting the core

Rewrote the core to make sure all operation is done logically, within
the objects and more efficiently. Also added validation.