Refactored/cleaned up parsing code a bit