Revert "Revert "replace long chain of ifs with some elifs and add 404 exception""

This reverts commit b7ecc0866f35696de69cd727ec7ccb19c60e8cf2.