Fixing bugs. Most functionality in place