Refactor and fix browser support