Added type annotations and reordered functions to make code more readable