lots of fixes, implemented self test