cleaned up code, double checked functionality