Fixed variable bug and if logic