cleaned up code, adjusted for ios7