Splitting up

1. Finish the messy function split.
2. small bug fixes.