first working version
fixed several bugs
more changes to code