add check for empty pieces in getPieces and Refactor