Revert 977c186..71e5f5d

This rolls back to commit 977c1868d5c4e8ec282a2d7445a9e17f33ef8dc5.
Reorganized some code