You should refactor each part of your algorithm in methods, this would be easier to follow