fixed a lot of ugly code