Fix type and reorder code