add main code
a lot of fixes