Adding finalized implementation. Cleaned up most of the dead code