Leader election works with some optimization. Code needs clean up though