translated up to line 2485, fixes #10
translated and refactored up to line 2966