Fix beacon spawn bug and refactor code