logic fix on retrieve function