some cleanup
moved debug code to function
moved helper functions to pxd
.tweaks