[breaking] Refactor code to fix edge cases and improve API usability