Fixes and refactor on leader election