fixed several bugs. Not finished