UPDATE: Changed argument from 'grid' to 'table'
UPDATE: Refactored code