Some bug fixes
Initial working version.