Fixed wrong logic + some debug info