Refactored defines into functions
extracted types into separate header
some reorganizations