added fault-tolerance and refactored