Rewrote the core to simplify everything. Added tests