Some bugfixes and code cleanup
Some more cleanup and initial if support
Fixed some bugs