Refactor code and fix bugs in battle controller