Fixed the code, I think it's ready. Comments needed.