Made code more fail safe and debug friendly