Working version

Some bug fixes. Good working version.