Fixed more bugs. Not yet tested