Both versions fixed, working. Second version optimized