removed duplicate code
implemented optional margin so that no point overflows