error checking added. Some bugfixing as well.