add debug functionality
fixed order of functions and some bugs
Corrected port