Added error checking and fixed getFreeBlock's return value