Reorganized code after long hiatus

Will be doing a lot of changes later
including deleting and rewriting code.