Fix for bug 127 and 345 as part of it

Fix for bug 127 and 345 as part of it
http://bugs.skysql.com/show_bug.cgi?id=127
http://bugs.skysql.com/show_bug.cgi?id=345