Refactor design and secure API