Bug fix causing error 500 in some cases