Simplified code a bit, removed dead code