added some optimization after conversation with Bookis