Big re-arrangement

Re-arranged almost all, and make it more abstract.