big refactoring - see pseudo-documentation for an overview
(also turned on debug builds)
removed debug code