added exceptions, memory leaks not fixed yet